package com.zh.baselibrary.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

/**
 * @author EnzoChan
 * @create 2019/3/10
 * @desc:
 */
public class PhoneUtil {

    public static String ICCID = "";
    public static String getUUID(Context mContext) {

        TelephonyManager TelephonyMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String szImei = TelephonyMgr.getDeviceId();
        if(szImei==null||szImei.equals("")){
            szImei= Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return szImei;

    }

    public static String getIccID(Context mContext) {
        if(!TextUtils.isEmpty(ICCID)&&ICCID.length() ==20)
            return ICCID;
        String[] iccID = {""};
//        TelephonyManager TelephonyMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
//        @SuppressLint("MissingPermission") final String[] iccID = {TelephonyMgr.getSimSerialNumber()};
//        LogUtils.e("iccID:" + iccID[0]);
//        if ( !TextUtils.isEmpty(iccID[0])&&iccID[0].length() != 20) {
//            LogUtils.e("iccID3");
            readTest("/dev/ttyUSB3/",iccID);
//        }
        if ( !TextUtils.isEmpty(iccID[0])&&iccID[0].length() != 20) {
            LogUtils.e("iccID3");
            readTest("/dev/ttyUSB4/",iccID);
        }

        ICCID = iccID[0];
        return iccID[0];
    }
    public static void readTest(String path, final String[] data){
        try {
            final RandomAccessFile localRandomAccessFile = new RandomAccessFile(path, "rw");
            ;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        localRandomAccessFile.writeBytes("at+qccid" + "\r\n");
                        byte[] arrayOfByte = new byte[1024];
                        int readSize = 0;
                        while ((readSize = localRandomAccessFile.read(arrayOfByte)) == -1) {

                        }
                        String response = new String(arrayOfByte).substring(0, readSize);
                        LogUtils.e(response);
                        int index = response.indexOf("+QCCID:");
                        if (index != -1 && response.length() > index + 28)
                            data[0] = response.substring(index + 8, index + 28);
                        localRandomAccessFile.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            SystemClock.sleep(50);
        //    localRandomAccessFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }


    public static String getLineNumber(Context mContext) {

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String iccId = tm.getSubscriberId();

        return iccId;
    }

    public static int getLocalVersion(Context ctx) {
        int localVersion = 0;
        try {
            PackageInfo packageInfo = ctx.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ctx.getPackageName(), 0);
            localVersion = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }


    public static String getVersionName(Context ctx) {
        String localVersion = null;
        try {
            PackageInfo packageInfo = ctx.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ctx.getPackageName(), 0);
            localVersion = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }


}
